<?php

namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class UserpaisysController extends AdminbaseController {
    public function index() {
 
        $p=1;
        if (isset($_GET['p'])){
            $p=$_GET['p'];
        }
        $queryDesc='order by fen desc';
        
        $postData=array();
        $where='';
        $postData['p']=$p;

        $Model = M();
        
        if (isset($_GET['gid'])){
            $_SESSION['gameid']=$_GET['gid'];
        }
        $gameInfo=$Model->query("SELECT id,name FROM `z_game`   ");
        if (isset($_SESSION['gameid'])){
            $queryTable=$Model->query("SELECT id,name FROM `z_game` where id=".$_SESSION['gameid']." limit 0,1 ");
            $tableName='y'.$queryTable[0]['id'];
        }else{
            $queryTable=$Model->query("SELECT id,name FROM `z_game` where is_open=1 limit 0,1 ");
            $tableName='y'.$queryTable[0]['id'];
        }
        $gameId=$queryTable[0]['id'];
        $gameName=$queryTable[0]['name'];
        
        $result = $Model->query("SHOW TABLES LIKE '".$tableName."_team' ");
        if (empty($result)){
            $tableName='y1'; //表不存在
            $where=" where id='0' ";
        }
      
        $postData['name']='';
        if (isset($_POST['name']) && !empty($_POST['name'])){
            
            $page = $this->page(1, 10, $p);
            $name=addslashes(trim($_POST['name']));
            $postData['name']=$_POST['name'];
            $getU=$Model->query("SELECT openid,beizhu,phone,nickname,headimg FROM `".$tableName."_users` where name like '%".$name."%' or phone='".$name."'  limit 0,1 ");
            
           
            $getFen=$Model->query("SELECT * FROM `".$tableName."_fen` where openid='".$getU[0]['openid']."' limit 0,1  ");
            
            $user_pid=1;
            $pid=1;
            $uInfo=$Model->query("SELECT openid FROM `".$tableName."_fen` ".$queryDesc."  ");
            foreach ($uInfo as $v){
                if($v['openid'] == $getU[0]['openid']){
                    $user_pid=$pid;
                    break;
                }
                $pid++;
            }
            
            $theData=array();
            $theData[]=array('openid'=>$getU[0]['openid'],'pid'=>$user_pid,'phone'=>$getU[0]['phone'],'nickname'=>base64_decode($getU[0]['nickname']),'headimg'=>$getU[0]['headimg'],
                'fen'=>$getFen[0]['fen'],'beizhu'=>$getU[0]['beizhu'],'ishuojiang'=>$getFen[0]['ishuojiang']);
            
            
        }else{
            $query=$Model->query("SELECT count(id) as count FROM `".$tableName."_fen` ".$where."  ");
            $count=$query[0]['count'];
            $page = $this->page($count, 10, $p);
            $uInfo=$Model->query("SELECT * FROM `".$tableName."_fen` ".$where."  ".$queryDesc." limit ".(($p-1)*10).",10 ");
            $theData=array();
            $pid=1 + (($p-1)*10);
            foreach ($uInfo as $v){
                $getU=$Model->query("SELECT beizhu,phone,nickname,headimg FROM `".$tableName."_users` where openid='".$v['openid']."' limit 0,1 ");
                $theData[]=array('openid'=>$v['openid'],'pid'=>$pid,'phone'=>$getU[0]['phone'],'nickname'=>base64_decode($getU[0]['nickname']),'headimg'=>$getU[0]['headimg'],
                    'fen'=>$v['fen'],'beizhu'=>$getU[0]['beizhu'],'ishuojiang'=>$v['ishuojiang']);
                $pid++;
            }
        }
        
        
        
        
        $this->assign("postData", $postData);
        $this->assign("page", $page->show('Admin'));
        $this->assign("thedata", $theData);
        $this->assign("gameName", $gameName);
        $this->assign("gameId", $gameId);
        $this->assign("gameInfo", $gameInfo);

        $this->display();
    }
    
    public function add() {

        $this->display();
    }
    
   
    
    public function delete() {
        if (isset($_GET['id']) && !empty($_GET['id'])){
            $Model = M("game", 'z_');
            $data=array();
            if ($Model->delete($_GET['id'])!==false) {
                $this->success("删除成功！");
            } else {
                $this->error("删除失败！");
            } 
        }
    }
  
    public function edit() {
        if (isset($_GET['id'])){
            $id=$_GET['id'];
        }else{
            exit;
        }
        $p=1;
        if (isset($_GET['p'])){
            $p=$_GET['p'];
        }
        $Model = M();
        
        $queryTable=$Model->query("SELECT id FROM `z_game` where is_open=1 limit 0,1 ");
        $tableName='y'.$queryTable[0]['id'];

        $thedata=$Model->query("SELECT beizhu,openid,phone,nickname,headimg FROM `".$tableName."_users` where openid='".$id."' limit 0,1 ");
        $nickname=base64_decode($thedata[0]['nickname']);
        $thedata[0]['nickname']=$nickname;

        $getdata=$Model->query("SELECT 	ishuojiang FROM `".$tableName."_fen` where openid='".$id."' limit 0,1 ");
        
        $thedata[0]['ishuojiang']=$getdata[0]['ishuojiang'];
        
        
        $this->assign("p", $p);
        $this->assign("thedata", $thedata[0]);
         
        $this->display();
    }
     
   
    public function editpost() {
 
        if (isset($_POST['beizhu']) && isset($_POST['option_id'])){
            $Model = M();
            $queryTable=$Model->query("SELECT id FROM `z_game` where is_open=1 limit 0,1 ");
            $tableName='y'.$queryTable[0]['id'];
            
            if (empty($_POST['ishuojiang'])){
                $Model->execute(" update `".$tableName."_fen`  SET `ishuojiang`=NULL where openid='".$_POST['option_id']."'  ");
            }else{
                $Model->execute(" update `".$tableName."_fen`  SET `ishuojiang`=1  where openid='".$_POST['option_id']."'  ");
            }
            $beizhu=addslashes(trim($_POST['beizhu']));
            $Model->execute("update `".$tableName."_users` set beizhu='".$beizhu."'  where openid='".$_POST['option_id']."' ");
            
            $info=json_encode(array('status'=>'yes'));
            echo $info;
        }
    }
 
}
